/**
 * Created by vincent on 2017/9/11.
 */
moduleStudyMap.controller("StudyMapController", function ($scope, $log, $state, ProductResource, UserStudyResource, LoadingService, ConstantService, DialogService, StudyMapResource) {
    $scope.$on("$viewContentLoaded", function () {
        pageOnLoad();
        $scope.currentState = $state.current;
        if ($scope.currentState.name === "sz.studyMap") {
            ConstantService.addState($state.current, true);
        }
    });

    $scope.queryPara = {};
    $scope.queryStudyMap = function (pageNo) {
        //LoadingService.show();
        StudyMapResource.queryPage({pageNo: pageNo - 1, pageSize: 10}, $scope.queryPara, function (queryResult) {
            $log.debug("地图查询结果", queryResult);
            $scope.queryResult = queryResult;
            $scope.pagination.setPage(queryResult, pageNo);
            //LoadingService.hide();
        });
    };
    $scope.pagination = new Pagination();
    $scope.pagination.setPageClickFn($scope.queryStudyMap);
    $scope.queryStudyMap($scope.pagination.currentPageNo);

    $scope.addStudyMap = function () {
        DialogService.open("addStudyMap");
    };

    $scope.editStudyMap = function (studyMap) {
        $scope.currentStudyMap = studyMap;
        DialogService.open("editStudyMap", $scope);
    };

    $scope.setupClassGroup = function (studyMap) {
        ConstantService.setItem("currentStudyMapId", studyMap.id);
        $state.go("sz.studyMap.classGroup");
    };

    $scope.toRoute = function (map) {
        $scope.currentStudyMap = map;
        DialogService.open("setStudyMapRoute", $scope);
    };

    $scope.clearUserStudy = function (studyMap) {
        Alert.confirm("是否确认要执行该操作？", function () {
            var bean = {
                studyMapId: studyMap.id
            };
            UserStudyResource.clearForStudyMap(bean, {}, function () {
                $log.debug("清理用户信息");
                DialogService.close();
                Alert.success("清理用户信息成功！");
                //LoadingService.hide();
            });
        });
    };

    $scope.clone = function (studyMap) {
        Alert.confirm(function () {
            var bean = {
                studyMapId: studyMap.id
            };
            StudyMapResource.clone({}, bean, function () {
                $log.debug("清理用户信息");
                DialogService.close();
                Alert.success();
                //LoadingService.hide();
            });
        });
    }

    $scope.updateProductStatus = function (map, productStatus) {
        var bean = {
            id: map.id,
            status: productStatus,
            type: "STUDY_MAP_ID"
        };
        ProductResource.updateStatus({}, bean, function () {
            $log.debug("更新产品状态");
            DialogService.close();
            Alert.success("更新产品状态成功！");
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        })
    }

    $scope.refresh = function () {
        $scope.queryPara = {};
    };

    $scope.toQRCode = function (map) {
        $scope.qrCodeBean = {
            id: map.id,
            fieldName: "STUDY_MAP_ID"
        };
        DialogService.open("productQRCode", $scope);
    }
});